import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/3/16
 * \* Time: 8:46
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class TestDemo {

    public static void heapSort(int[] array){
        //1.建堆
        createheap(array);
        int end = array.length-1;
        //2、交换然后调整 O(N * log N)
        while (end > 0) {
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }

    private static void createheap(int[]array){
        for(int parent = (array.length-1-1)/2;parent>=0; parent--){
            shiftDown(array,parent,array.length);
        }
    }
    private static void shiftDown(int[]array,int parent,int len){
        int child = 2*parent +1;
        while (child < len){
            if(child+1 < len && array[child] < array[child+1]){
                child++;
            }
            if (array[child] > array[parent]){
                swap(array,child,parent);
                parent = child;
                child = 2*parent +1;
            }else {
                break;
            }
        }
    }

    public static void mergeSort1(int[] array){
        int nums = 1;//每组的数据个数
        while (nums < array.length) {
            //数组每次都要进行遍历,确定要归并的区间
            for (int i = 0; i < array.length; i += nums*2) {
                int left = i;
                int mid = left+nums-1;
                //防止越界
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                int right = mid+nums;
                //防止越界
                if(right >= array.length) {
                    right = array.length-1;
                }
                //小标确定之后，进行合并
                merge(array,left,mid,right);
            }
            nums *= 2;
        }
    }

    public static void mergeSort(int[] array){
        mergeSortInternal(array,0,array.length-1);
    }
    private static void  mergeSortInternal(int[]array,int low ,int high){
        if(low >= high) return;
        int mid = low + ((high - low) >>> 1);
        mergeSortInternal(array,low,mid);
        mergeSortInternal(array,mid+1,high);
        merge(array,low,mid,high);
    }
    private static void merge(int[] array,int low,int mid,int high) {
        int[] tmp = new int[high-low+1];
        int k = 0;//

        int s1 = low;
        int e1 = mid;
        int s2 = mid+1;
        int e2 =  high;
        while (s1 <= e1 && s2 <= e2) {
            if(array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmp[k++] = array[s2++];
        }
        //拷贝tmp数组的元素 放入原来的数组array当中
        for (int i = 0; i < k; i++) {
            array[i+low] = tmp[i];
        }
    }




    public static void shell(int[]array,int gap){
        for(int i =gap ; i<array.length;i++){
            int tmp = array[i];
            int j = i-gap;
            for(;j>=0 ; j-=gap){
                if(array[j] >tmp){
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    public static void shellSort(int[]array){
        int[] drr = {5,3,1};
        for (int i =0;i<drr.length;i++){
            shell(array,drr[i]);
        }
    }

    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> map = new HashMap<>();
        for(int i =0; i <nums.length; i++){
            map.put(nums[i],i);
        }
        for (int i = 0;i<nums.length; i++){
            int n = target-nums[i];
            if (map.containsKey(n)){
                return new int[]{i,map.get(n)};
            }
        }
        return new int[]{};
    }

    //快速排序：取一个数作为基准，将比它小的数放左边，比它大的数放右边；再各自从左边或者右边取一个数作为基准，重复上面过程，直到
    //为1

    public static void insertQuick(int[] array){
        quick(array,0,array.length-1);
    }
    static void quick(int[]array,int left,int right){
        if(left >= right){
            return;
        }
        int pivot = partition(array,left,right);
        quick(array,left,pivot-1);
        quick(array,pivot+1,right);

    }
    static void quick1(int[]array){
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length-1;
        int pivot = findMidValIndex(array,left,right);
        if(pivot > left+1){
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot < right-1){
            stack.push(pivot+1);
            stack.push(right);
        }
        while (!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();

            pivot = findMidValIndex(array,left,right);
            if(pivot > left+1){
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot < right-1){
                stack.push(pivot+1);
                stack.push(right);
            }
        }

    }


    static int partition(int[]array, int start, int end){
        int tmp = array[start];
        while(start < end){
            while (start < end && array[end] >= tmp) {
                end--;
            }
            array[start] = array[end];
            while (start < end && array[start] <= tmp){
                start++;
            }
            array[end] = array[start];
        }
        array[start] = tmp;
        return start;
    }


    private static int findMidValIndex(int[] array,int start,int end) {
        int mid = start + ((end-start) >>> 1);
        if(array[start] < array[end]) {
            if(array[mid] < array[start]) {
                return start;
            }else if(array[mid] > array[end]) {
                return end;
            }else {
                return mid;
            }
        }else {
            if(array[mid] > array[start]) {
                return start;
            }else if(array[mid] < array[end]) {
                return end;
            }else {
                return mid;
            }
        }
    }






    //简单选择排序,同n-i次关键字比较，从n-i+1的记录中选出最小的关键字，并和第i个记录交换
    public static void insertSelect(int[]array){
        for (int i = 0; i < array.length-1; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[i] > array[j]){
                    swap(array,i,j);
                }
            }
        }

    }


    //冒牌排序： 将临近的两个元素两两比较，小的则往上冒泡，直到没有反序
    public static void insertBubble(int [] array){
        for (int i = 0;i< array.length-1; i++){
            for (int j = 0; j < array.length -1; j++) {
                if(array[j] > array[j+1]){
                    swap(array,j+1,j);
                }
            }
        }
    }

    //插入排序：将一个记录插入到已经排好序的有序表中，从而得到一个新增为1的表
    public static void insertSort(int [] array){
        for(int i =1 ; i<array.length; i++){
            int tmp = array[i];
            int j = i-1;
            for (;j>=0;j--){
                if(tmp > array[j]){
                    break;
                }else {
                    array[j+1] = array[j];
                }
            }
            array[j+1] = tmp;
        }
    }

    public static void main(String[] args) {
        int [] array = {2,3,4,1,33,22,43,21,8,9};
        //insertSort(array);
       // insertBubble(array);
       // insertSelect(array);
      //  insertQuick(array);
       // shellSort(array);
        mergeSort1(array);
        print(array);

    }

    static void print(int[]array){
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");

        }
    }
    static void swap(int[] array, int i, int j ){
        int tmp = array[i];
        array[i] = array[j];
        array[j] =tmp;
    }
}
